package com.wanmait.hotelManage.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.wanmait.hotelManage.util.DBHelper;
import com.wanmait.hotelManage.vo.Vip;

public class VipDAO {
	private VipDAO() {}
	private static VipDAO vipDao=new VipDAO();
	public static VipDAO getVipDao() {
		return vipDao;
	}
	public List<Vip> list() {
		// TODO Auto-generated method stub
		List<Vip> vips=new ArrayList<Vip>();
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		String sql="select * from vip where enable=1";
		
		try {
			conn=DBHelper.getDBHelper().getConn();
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()) {
				Vip vip=new Vip();
				vip.setDiscount(rs.getDouble("discount"));
				vip.setEnable(true);
				vip.setId(rs.getInt("id"));
				vip.setInputTime(rs.getTimestamp("input_time"));
				vip.setPrice(rs.getDouble("price"));
				vip.setTitle(rs.getString("title"));
				vips.add(vip);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBHelper.getDBHelper().close(rs, ps, conn);
		}
		
		return vips;
	}
	//根据id找vip
	public Vip findVipById(int id) {
		// TODO Auto-generated method stub
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		Vip vip=null;
		String sql="select * from vip where id=?";
		
		try {
			conn=DBHelper.getDBHelper().getConn();
			ps=conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs=ps.executeQuery();
			if(rs.next()) {
				vip=new Vip();
				vip.setDiscount(rs.getDouble("discount"));
				vip.setEnable(true);
				vip.setId(rs.getInt("id"));
				vip.setInputTime(rs.getTimestamp("input_time"));
				vip.setPrice(rs.getDouble("price"));
				vip.setTitle(rs.getString("title"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBHelper.getDBHelper().close(rs, ps, conn);
		}
		
		return vip;
	}
	
	//修改car信息
	public void update(Vip vip) {
		// TODO Auto-generated method stub
		String sql="update vip set title=?,discount=?,price=? where id=?";
		DBHelper.getDBHelper().executeUpdate(
				sql, 
				vip.getTitle(),
				vip.getDiscount(),
				vip.getPrice(),
				vip.getId()
				);
	}
	//删除
	public void delete(int id) {
		// TODO Auto-generated method stub
		String sql="update vip set enable=0 where id=?";
		DBHelper.getDBHelper().executeUpdate(sql, id);
	}
	//添加
	public void insert(Vip vip) {
		// TODO Auto-generated method stub
		String sql="insert into vip(title,price,discount) values(?,?,?)";
		DBHelper.getDBHelper().executeUpdate(
				sql,
				vip.getTitle(),
				vip.getPrice(),
				vip.getDiscount()
				);
	}
	public Double findprice(int vip) {
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		String sql="select discount from vip where id=?";
		conn=DBHelper.getDBHelper().getConn();
		try {
			ps=conn.prepareStatement(sql);
			ps.setInt(1, vip);
			rs=ps.executeQuery();
			if(rs.next()) {
				Double in=rs.getDouble("discount");
				return in;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBHelper.getDBHelper().close(rs, ps, conn);
		}
		return null;
	}
//public static void main(String[] args) {
//	Double a=VipDAO.getVipDao().findprice(1);
//	System.out.println(a);
//}
}
